/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * JanelaModificarLei.java
 *
 * Created on 21/05/2011, 22:20:03
 */

package gui;

import java.util.List;
import javax.swing.Action;
import javax.swing.JOptionPane;
import modelo.Lei;

/**
 *
 * @author DOCUMENTOS
 */
public class JanelaModificarLei extends javax.swing.JFrame implements UI {


    /** Creates new form JanelaModificarLei */
    public JanelaModificarLei() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
  private void initComponents() {

    labelNumeroLei = new javax.swing.JLabel();
    campoNumeroLei = new javax.swing.JTextField();
    labelProcesso = new javax.swing.JLabel();
    campoProcesso = new javax.swing.JTextField();
    labelEmenta = new javax.swing.JLabel();
    jScrollPane1 = new javax.swing.JScrollPane();
    campoEmenta = new javax.swing.JTextArea();
    labelTexto = new javax.swing.JLabel();
    botaoAlterar = new javax.swing.JButton();
    barraTexto = new javax.swing.JScrollPane();
    campoTexto = new javax.swing.JTextPane();
    barraFerramentas = new component.MXTextToolbar();

    setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

    labelNumeroLei.setText("Nº Lei:");

    labelProcesso.setText("Processo:");

    labelEmenta.setText("Ementa:");

    campoEmenta.setColumns(20);
    campoEmenta.setRows(5);
    jScrollPane1.setViewportView(campoEmenta);

    labelTexto.setText("Texto:");

    botaoAlterar.setText("Alterar");
    botaoAlterar.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        botaoAlterarActionPerformed(evt);
      }
    });

    campoTexto.setContentType("text/html");
    barraTexto.setViewportView(campoTexto);

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(layout.createSequentialGroup()
        .addContainerGap()
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
          .addGroup(layout.createSequentialGroup()
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
              .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                .addComponent(labelProcesso)
                .addComponent(labelNumeroLei)
                .addComponent(labelTexto))
              .addGroup(layout.createSequentialGroup()
                .addGap(8, 8, 8)
                .addComponent(labelEmenta)))
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
              .addComponent(barraTexto, javax.swing.GroupLayout.DEFAULT_SIZE, 996, Short.MAX_VALUE)
              .addComponent(campoNumeroLei, javax.swing.GroupLayout.DEFAULT_SIZE, 996, Short.MAX_VALUE)
              .addComponent(campoProcesso, javax.swing.GroupLayout.DEFAULT_SIZE, 996, Short.MAX_VALUE)
              .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 996, Short.MAX_VALUE)
              .addComponent(barraFerramentas, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
          .addComponent(botaoAlterar, javax.swing.GroupLayout.Alignment.TRAILING))
        .addContainerGap())
    );
    layout.setVerticalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(layout.createSequentialGroup()
        .addContainerGap()
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
          .addComponent(campoNumeroLei, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
          .addComponent(labelNumeroLei))
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
          .addComponent(campoProcesso, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
          .addComponent(labelProcesso))
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
          .addComponent(labelEmenta)
          .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 82, javax.swing.GroupLayout.PREFERRED_SIZE))
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
          .addComponent(labelTexto)
          .addComponent(barraFerramentas, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addComponent(barraTexto, javax.swing.GroupLayout.DEFAULT_SIZE, 315, Short.MAX_VALUE)
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addComponent(botaoAlterar)
        .addContainerGap())
    );

    pack();
  }// </editor-fold>//GEN-END:initComponents

    private void botaoAlterarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botaoAlterarActionPerformed
      controladorCadastroLei.setUI(this);
      try{
      if (lei.getId() == null) {
          exibirMensagemErro("A Lei deve está Cadastrada");
      } else {
              lei.setTexto(campoTexto.getText());
              lei.setEmenta(campoEmenta.getText());
              lei.setNumeroLei(campoNumeroLei.getText());
              lei.setProcesso(campoProcesso.getText());
              if (campoEmenta.getText().equalsIgnoreCase("") || campoNumeroLei.getText().equalsIgnoreCase("") || campoProcesso.getText().equalsIgnoreCase("") || campoTexto.getText().equalsIgnoreCase("")) {
                  exibirMensagemErro("Preencha os campos obrigatórios");
              } else {
                  controladorCadastroLei.alterar(lei);
                  campoEmenta.setText(null);
                  campoTexto.setText(null);
                  campoProcesso.setText(null);
                  campoNumeroLei.setText(null);
                  lei.setId(null);
              }
         }
      }catch(RuntimeException e){
          exibirMensagemErro("A lei deve está cadastrada, Use o botão Alterar Lei");
      }
    }//GEN-LAST:event_botaoAlterarActionPerformed

  // Variables declaration - do not modify//GEN-BEGIN:variables
  private component.MXTextToolbar barraFerramentas;
  private javax.swing.JScrollPane barraTexto;
  private javax.swing.JButton botaoAlterar;
  private javax.swing.JTextArea campoEmenta;
  private javax.swing.JTextField campoNumeroLei;
  private javax.swing.JTextField campoProcesso;
  private javax.swing.JTextPane campoTexto;
  private javax.swing.JScrollPane jScrollPane1;
  private javax.swing.JLabel labelEmenta;
  private javax.swing.JLabel labelNumeroLei;
  private javax.swing.JLabel labelProcesso;
  private javax.swing.JLabel labelTexto;
  // End of variables declaration//GEN-END:variables
    Controlador controladorCadastroLei;
    private Lei lei;
    
    public Controlador getControlador() {
        return controladorCadastroLei;
    }

    public void setControlador(Controlador c) {
        controladorCadastroLei = c;
    }

    public void exibirMensagem(String mensagem) {
        JOptionPane.showMessageDialog(this, mensagem);
    }

    public void exibirMensagemErro(String mensagem) {
        JOptionPane.showMessageDialog(this, mensagem);
    }

    public void mostrarObjeto(Object o) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void mostrarObjetos(List l) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void setarCampos(Lei l) {
        lei = l;
        campoEmenta.setText(lei.getEmenta());
        campoNumeroLei.setText(lei.getNumeroLei());
        campoProcesso.setText(lei.getProcesso());
        campoTexto.setText(lei.getTexto());
    }
}
